<?php

namespace app\admin\controller;

use app\admin\common\Base;
use think\Db;
use think\Session;

use app\admin\model\CustomerInvNumber as Model;

class Customerinvnumber extends Base
{
    public function index()
    {
        $data = $this->request->param();

        if (isset($data['ajax']) && $data['ajax'] == 100) {
            extract($data);
            $map = '';
            $page = isset($data['page']) ? $data['page'] : 1;
            $page = intval($page);
            $limit = isset($data['rows']) ? $data['rows'] : 10;
            $limit = intval($limit);
            $start = $limit * ($page - 1);

            $sql = Db::name('customer_inv_number');
            if (isset($goods_id) && !empty($goods_id)) $sql = $sql->where('goods_id', $goods_id);
            if (isset($customer_id) && !empty($customer_id)) $sql = $sql->where('customer_id', $customer_id);
            $sql = $sql->where('is_del', 0)->where('enterprise_id', $this->EnterpriseId);
            $list0 = $sql->order('id asc')->select();
            $count = count($list0);
            $list = array_slice($list0, $start, $limit);

            foreach ($list as $key => &$value) {
                $customer = Db::name('customer')->where('id', $value['customer_id'])->find();
                $value['customer_name'] = $customer['sn'] . ' ' . $customer['name'];

                $goods = Db::name('goods')->where('id', $value['goods_id'])->find();
                $value['goods_name'] = $goods['sn'] . ' ' . $goods['name'];

                $value['unit_name'] = Db::name('unit')->where('id', $value['unit_id'])->value('name');
            }

            return $this->grid_data($list, $count, $page, $limit);
        }
        return $this->fetch();
    }

    public function add()
    {
        $category = Db::name('customer')->where('status', 0)->where('is_del', 0)->select();
        $vip = Db::name('customer_vip')->where('is_del', 0)->select();
        $staff = Db::name('auth_user')->where('is_del', 0)->select();
        $this->assign('category', $category);
        $this->assign('vip', $vip);
        $this->assign('staff', $staff);
        return $this->fetch();
    }

    //编辑栏目1-1
    public function edit($id)
    {
        $info = Db::name('customer_inv_number')->where('id', $id)->find();
        $category = Db::name('customer')->where('status', 0)->where('is_del', 0)->select();

        $info['goods_name'] = Db::name('goods')->where('id', $info['goods_id'])->value('name');
        $info['unit_name'] = Db::name('unit')->where('id', $info['unit_id'])->value('name');

        $this->assign('info', $info);
        $this->assign('category', $category);
        return $this->fetch();
        // return $this->fetch();
    }
    private function handleData($basedata, $data)
    {
        foreach ($data as $key => $value) {
            unset($data[$key]['id']);
            $data[$key]['customer_id'] = $basedata;
            $data[$key]['enterprise_id'] = $this->EnterpriseId;

            if (!isset($value['number']) || empty($value['number'])) {
                unset($data[$key]);
            }
        }
        return $data;
    }

    //编辑栏目1-2
    public function save()
    {
        $data = $this->request->param();
        // return $data;
        $basedata = $data['customer_basedata'];
        $extdata = $data['customer_extdata'];
        $model = new Model();
        $insertData = $this->handleData($basedata['category_id'], $extdata);
        // halt($insertData);
        if ($insertData) {
            if (isset($basedata['id']) && !empty($basedata['id'])) {
                $result = $model->allowField(true)->save($insertData[0], ['id' => $basedata['id']]);
            } else {
                $result = $model->allowField(true)->saveAll($insertData);
            }
            if (!$result) {
                return json(['status' => 0, 'msg' => '操作失败']);
            } else {
                return json(['status' => 1, 'msg' => '操作成功']);
            }
        }
    }

    //删除栏目1-1
    public function del($id)
    {
        $del = Db::name('customer_inv_number')->where('id', $id)->update(["is_del" => 1]);
        if ($del) {
            return json(['status' => 0, 'msg' => '删除失败']);
        }
        return json(['status' => 1, 'msg' => '删除成功']);
    }
    //添加商品
    public function goods()
    {
        return $this->fetch();
    }
}
